package com.tg.demo.web;

import com.tg.demo.config.SysUserContext;
import com.tg.demo.entity.Activity;
import com.tg.demo.entity.Hobby;
import com.tg.demo.entity.Score;
import com.tg.demo.entity.User;
import com.tg.demo.service.ActivityService;
import com.tg.demo.service.HobbyService;
import com.tg.demo.service.ScoreService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * IndexController
 *
 * @author yuanjin
 * @date 2019/3/21
 */
@Controller
@RequestMapping
public class IndexController {

    @Resource
    private ActivityService activityService;

    @Resource
    private ScoreService scoreService;

    @Resource
    private HobbyService hobbyService;

    @GetMapping("/")
    public String index(HttpServletRequest request, Model model) {
        User user = SysUserContext.getSysUser();
        if (null != user) {
            model.addAttribute("user", user);
            String userHobbies = user.getHobbies();
            Hobby hobby = hobbyService.recommendValue(userHobbies);
            model.addAttribute("hobby", hobby);
            List<Activity> activities = activityService.getListToUser(user.getUserCode());
            if (null != activities && !activities.isEmpty()) {
                model.addAttribute("activityShow", 1);
            }
            Score score = scoreService.getByUserCode(user.getUserCode());
            model.addAttribute("activities", activities);
            model.addAttribute("score", score);
        }
        return "welcome";
    }
}